//
//  ListConfigure.swift
//  Extend
//
//  Created by shufang zou on 2017/8/4.
//  Copyright © 2017年 敬信. All rights reserved.
//

public typealias FormViewClick = () -> ();


public class ListConfigureM {
    var limit: Int = ConstantsEx.kListLimit;
    var type: Int?;
    var name: String?;
    var hint: String?;
    var text: String?;
    var regex: String?;
    var height: Int?;
    var related: Any?;
    var image: UIImage?;
    var bgColor: UIColor?;
    
    var click: OnFormItemClick?;
    var viewClick: (Int, FormViewClick)?;
    
    fileprivate init() {
    }
    
    open func build(_ type: Int) -> FormEx? {
        return nil;
    }
    
    public class func newBuilder() -> ListConfigureBuilder {
        return ListConfigureBuilder();
    }
}

public class ListConfigureBuilder {
    
    var limit: Int?;
    var type: Int?;
    var name: String?;
    var hint: String?;
    var text: String?;
    var regex: String?;
    var height: Int?;
    var related: Any?;
    var image: UIImage?;
    var bgColor: UIColor?;
    var cellClick: OnFormItemClick?;
    var cellViewMap: Dictionary<Int, FormViewClick> = Dictionary();
    
    fileprivate init() {
    }
    
   
    public func limit(_ t: Int) -> Self {
        limit = t;
        return self;
    }
    public func type(_ t: Int) -> Self {
        type = t;
        return self;
    }
    public func name(_ n: String) -> Self {
        name = n;
        return self;
    }
    public func hint(_ h: String) -> Self {
        hint = h;
        return self;
    }
    public func text(_ t: String) -> Self {
        text = t;
        return self;
    }
    public func regex(_ r: String) -> Self {
        regex = r;
        return self;
    }
    public func height(_ h: Int) -> Self {
        height = h;
        return self;
    }
    public func related(_ r: Any) -> Self {
        related = r;
        return self;
    }
    public func image(_ i: UIImage) -> Self {
        image = i;
        return self;
    }
    public func bgColor(_ color: UIColor) -> Self {
        bgColor = color;
        return self;
    }
    
    public func cellClick(_ c: @escaping OnFormItemClick) -> Self {
        cellClick = c;
        return self;
    }
    
    public func cellClick(_ viewId: Int, _ click: @escaping FormViewClick) -> Self {
        cellViewMap.updateValue(click, forKey: viewId);
        return self;
    }
 
    public func build() -> FormEx? {
        let form = self.build(self.type!);
        self.setValues(form!);
        return form;
    }
    
   //    子类实现
    public func build(_ type: Int) -> FormEx? {

        return nil;
    }

    func setValues(_ cell : FormEx) {
        cell.type = type;
        cell.name = name;
        cell.hint = hint;
        cell.text = text;
        cell.regex = regex;
        cell.height = height;
        cell.related = related;
        cell.image = image;
        cell.bgColor = bgColor;
        cell.onClick = cellClick;
    }
    
    
    public func builder() -> ListConfigureM {
        let config = ListConfigureM();
        config.limit = limit!;
        config.type = type;
        config.name = name;
        config.hint = hint;
        config.text = text;
        config.regex = regex;
        config.height = height;
        config.related = related;
        config.image = image;
        config.bgColor = bgColor;
        
        if limit != nil {
            config.limit = limit!;
        }
        if type != nil {
            config.type = type!;
        }
        if name != nil {
            config.name = name!;
        }
        if hint != nil {
            config.hint = hint!;
        }
        if text != nil {
            config.text = text!;
        }
        if height != nil {
            config.height = height!;
        }
        if related != nil {
            config.related = related!;
        }
        if image != nil {
            config.image = image!;
        }
        if bgColor != nil {
            config.bgColor = bgColor!;
        }
        
        return config;
    }

    
}
